//
// Created by Administrator on 2020-2-13.
//

#include "DataStructre.h"
#include <stdio.h>
#include <string.h>

int GetNum();
void Swap(bookInfo * book_1, bookInfo * book_2);

// 排序函数
void sort(bookList * context){
    puts("输入1按照登录号排序，输入2按照价格排序:");
    int mode = GetNum();
    if (mode == 1){
        for (int i = 0; i < context->len; ++i) {
            int min = i;
            for (int j = i + 1 ; j < context->len; ++j) {
                if (context->list[j].bookId < context->list[min].bookId)
                    min = j;
            }
            Swap(&context->list[min], &context->list[i]);
        }
    } else {
        for (int i = 0; i < context->len; ++i) {
            int min = i;
            for (int j = i + 1; j < context->len; ++j) {
                if (context->list[j].price < context->list[min].price)
                    min = j;
            }
            Swap(&context->list[min], &context->list[i]);
        }
    }
}

// 两个结构体内存数据交换函数
void Swap(bookInfo * book_1, bookInfo * book_2){
    bookInfo temp;
    memcpy(&temp, book_1, sizeof(bookInfo));
    memcpy(book_1, book_2, sizeof(bookInfo));
    memcpy(book_2, &temp, sizeof(bookInfo));
}

// 返回一个int
int GetNum(){
    int val;
    scanf("%d", &val);
    return val;
}